{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 全排列\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19b4c5d5-80f5-448d-8ccd-d180dcca2b0e",
   "metadata": {},
   "source": [
    "该代码使用`itertools`库生成字母集合$\\{\"A\", \"B\", \"C\"\\}$的所有可能排列。具体来说，`itertools.permutations`函数生成集合中所有元素的全排列，并将不同顺序的排列视为不同的结果。对于包含三个元素的集合，排列的总数可以通过公式$P(n, n) = n!$计算，其中$n$为元素数量。因此，对于$n = 3$，排列总数为\n",
    "\n",
    "$$\n",
    "3! = 6\n",
    "$$\n",
    "\n",
    "即六种排列组合：\n",
    "\n",
    "$$\n",
    "(A, B, C), (A, C, B), (B, A, C), (B, C, A), (C, A, B), (C, B, A)\n",
    "$$\n",
    "\n",
    "代码依次输出这些排列，展示了如何生成所有元素的排列组合，这在需要考虑顺序的排列问题中具有广泛应用，例如组合数学和概率计算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6fc99140-fb5b-4335-8bd6-0951c998ca3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('A', 'B', 'C')\n",
      "('A', 'C', 'B')\n",
      "('B', 'A', 'C')\n",
      "('B', 'C', 'A')\n",
      "('C', 'A', 'B')\n",
      "('C', 'B', 'A')\n"
     ]
    }
   ],
   "source": [
    "## 排列所有3个字母的组合\n",
    "import itertools\n",
    "\n",
    "letters = \"ABC\"  # 定义字母集\n",
    "\n",
    "per = itertools.permutations(letters)  # 生成包含所有3个字母的排列\n",
    "\n",
    "for val in per:\n",
    "    print(val)  # 输出每一个排列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
